#include "usart.h"

void USARTConfig(void)
{
	//相关结构体
	GPIO_InitTypeDef GPIO_InitStructure;
	USART_InitTypeDef USART_InitStructure;
	NVIC_InitTypeDef NVIC_InitStructure;

	//时钟源
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);
	RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA,ENABLE);

	//第一步：配置GPIO引脚
	GPIO_PinAFConfig(GPIOA,GPIO_PinSource9,GPIO_AF_1);     //Tx
	GPIO_PinAFConfig(GPIOA,GPIO_PinSource10,GPIO_AF_1);    //Rx

	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;      //USART1_TX | USART1_RX
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;  
	GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;  
	GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(GPIOA, &GPIO_InitStructure);  

	GPIO_InitStructure.GPIO_Pin=GPIO_Pin_10;
	GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF;
	GPIO_InitStructure.GPIO_OType=GPIO_OType_PP;
	GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
	GPIO_InitStructure.GPIO_PuPd=GPIO_PuPd_UP;
	GPIO_Init(GPIOA,&GPIO_InitStructure);

	//第二步：配置串口
	USART_InitStructure.USART_BaudRate = 115200;    
	USART_InitStructure.USART_WordLength = USART_WordLength_8b;  
	USART_InitStructure.USART_StopBits = USART_StopBits_1;
	USART_InitStructure.USART_Parity = USART_Parity_No;  
	USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
	USART_InitStructure.USART_Mode = USART_Mode_Tx | USART_Mode_Rx; 
	USART_Init(USART1,&USART_InitStructure);  

	//第二步：配置中断
	/*****USART1 interrupt Config****/
	NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
	NVIC_InitStructure.NVIC_IRQChannelPriority = 0;
	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
	NVIC_Init(&NVIC_InitStructure);

	USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
	USART_Cmd(USART1,ENABLE);        
	
}

/*****************  发送一个字符 **********************/
void Usart_SendByte( USART_TypeDef * pUSARTx, uint8_t ch )
{
    /* 发送一个字节数据到USART1 */
    USART_SendData(pUSARTx,ch);

    /* 等待发送完毕  */
    while (USART_GetFlagStatus(pUSARTx, USART_FLAG_TXE) == RESET);
}
/*****************  发送字符串  **********************/
void Usart_SendString( USART_TypeDef * pUSARTx, char *str)
{
    unsigned int k=0;
    do 
    {
        Usart_SendByte( pUSARTx, *(str + k) );
        k++;
    } while(*(str + k)!='\0');
}

//#########################################################################

//发送buf
int USART1_SendBuf( uint8_t *p,uint16_t len )
{  		
  while(len--)
  {
    /*判断发送缓冲区是否为空*/
		while(!USART_GetFlagStatus(USART1,USART_FLAG_TXE));
		USART_SendData(USART1,*p++);
  }
  while(USART_GetFlagStatus(USART1, USART_FLAG_TC)==RESET);//发送完成
  return 1;	
}

 //重定向打印输出
 int fputc(int ch, FILE* stream)
 {
 	Usart_SendByte(USART1,(unsigned char)ch);
	
 	while(USART_GetFlagStatus(USART1,USART_FLAG_TXE)==RESET);

 	return ch;
 }


//串口中断函数
void USART1_IRQHandler()
{
	uint16_t temp;
	
	if(USART_GetITStatus(USART1,USART_IT_RXNE)!= RESET)   //接收数据寄存器非空中断。
	{
		USART_ClearITPendingBit(USART1,USART_IT_RXNE);
		temp = USART_ReceiveData(USART1);  
		USART_SendData(USART1,temp);
	}
}
















